# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import requests
import json


def send_request(request_url, content):
    """ 调用 NC API
    """

    head = {'Content-type': 'application/json'}
    requestdata = content.encode("utf-8")
    r = requests.post(request_url, data=requestdata, headers=head)
    # print(r)
    # print(r.text)
    return r.text


class NC(object):
    def __init__(self, base_url):
        self.base_url = base_url

    def call_request(self, url, content):
        request_url = self.base_url + url
        body = json.dumps(content)
        return send_request(request_url, body)

    def getxsfp(self, content):
        # 回传 发票信息
        url = '/fapiao/getxsfp'
        return self.call_request(url, content)


def test_getxsfp():
    data = [
        {
            # 发票头上的信息
            "bill_type": "1",    # 1:blue；2：red
            "action_type": "1",  # 1:正常；2：作废；

            "action_time": "yyyymmdd HH:MM:SS",  # 时间


            "goldtaxno": "037123456789",  # 税控发票代码
            "goldtaxcode": "18991234",  # 税控发票号码
            "checkcode": "12345678123456781234",  # 校验码
            "kprq": "2022-08-30",  # 开票日期
            "totalmoney":  200.00,  # 总金额
            "purchasername": "购方客户1",  # 客户

            # 发票明细行上的信息
            # 外部业务系统数据
            "saleinvoiceid": "0012",    # 发票主键
            "saleinvoicebid": "00013",  # 发票明细 主键. 唯一
            "amount": 87.00,     # 该行 不含税金额
            "taxamount": 13.00,  # 该行的 税
        }
    ]

    content = data

    result = nc.getxsfp(content)

    # {"result":2,"message":"传输失败！","error":null}
    # {"result":1,"message":"传输成功！"}

    print(result)


if __name__ == '__main__':

    base_url = 'http://219.146.120.3:8099'
    nc = NC(base_url)

    test_getxsfp()
